package org.skipper.samples.dubbo.consumer.impl;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.Constants;
import org.apache.dubbo.rpc.RpcContext;
import org.skipper.common.result.RpcResult;
import org.skipper.samples.client.DemoService;
import org.skipper.samples.dto.DemoDTO;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author shoujian
 * Date: 2022-02-14
 * Time: 7:01 PM
 */
@Component
@DubboService(version = "1.0.0")
public class ConsumerServiceImpl implements DemoService {

    @DubboReference(version = "1.0.0", group = "voyage-samples-dubbo-provider")
    DemoService demoService;

    @Override
    public RpcResult<DemoDTO> sayHello() {
        //模拟业务中使用rpc上下文
        String trafficSources = RpcContext.getContext().getAttachment("trafficSources");
        System.out.println("consumer = " + trafficSources);
        String dubboTag = RpcContext.getContext().getAttachment(Constants.FORCE_USE_TAG);
        System.out.println("dubboTag = " + dubboTag);
        RpcResult<DemoDTO> rpcResult = demoService.sayHello();

        System.out.println(rpcResult);
        return demoService.sayHello();
    }


    public static void main(String[] args) {
        Map<String, String> versionMap = new HashMap<>();
        versionMap.put("version", "11");
        final Set<Map.Entry<String, String>> attributes =
                Collections.unmodifiableSet(versionMap.entrySet());

        System.out.println(attributes);

        Map<String, String> metadata = new HashMap<>();
        metadata.put("version11", "11");
        if (metadata.entrySet().containsAll(attributes)) {
            System.out.println(attributes);
        }

    }
}
